Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace DHT with private peer discovery #34

Merged
merged 22 commits into from
Oct 31, 2024
Merged

Conversation

brewmaster012
Copy link
Collaborator

@brewmaster012 brewmaster012 commented Oct 25, 2024

should be used/merged with #31.

The public DHT can bring in unwanted connections that can overwhelm resources.
This PR replaces public libp2p K/DHT with custom private peer discovery via gossip among
the authenticated peers (see #31).

This addresses the issue that removing DHT can break the performance e2e test (since >2
zetaclients would not be able to discover other zetaclients via DHT peer discovery, thus
Keygen/Keysign will fail).

This discovery mechanism is adaptive and does not require any configuration except a
common bootstrap node upon startup. When it fails, zetaclients can fall back to hardcode the
correct peer information in the address_book.

Reviewer notes: because DHT rendezvous is removed, the interface of Communicator has
the field rendezvous removed. This affects most of the changes in the calling site and test cases.

Summary by CodeRabbit

  • New Features

    • Introduced a new PeerDiscovery struct for improved peer management in the P2P network.
    • Enhanced peer discovery process with better logging and connection handling.
  • Bug Fixes

    • Improved error handling in peer communication and state management.
  • Refactor

    • Streamlined NewCommunication and NewTss function signatures by removing unnecessary parameters.
    • Removed DHT-related functionalities to simplify the communication process.
  • Tests

    • Added new tests for peer discovery and updated existing tests to reflect changes in communication instantiation.
    • Enhanced test setups by removing hardcoded string parameters for communication instances.
  • Chores

    • Updated dependencies for better compatibility and performance.

Copy link

coderabbitai bot commented Oct 25, 2024

📝 Walkthrough

Walkthrough

The pull request introduces significant changes to the peer-to-peer communication framework by removing the Distributed Hash Table (DHT) functionality from the p2p/communication.go file. The Communication struct is modified, and a new PeerDiscovery struct is implemented in p2p/discovery.go to manage peer discovery without DHT. Additionally, various method signatures across multiple files are updated to reflect the removal of the rendezvous parameter and the introduction of whitelistedPeers. The dependency management in go.mod is also restructured, replacing several libraries.

Changes

File Change Summary
p2p/communication.go Removed DHT functionality, updated Communication struct and methods, added NewPeerDiscovery method. Signature updated for NewCommunication.
p2p/discovery.go Implemented PeerDiscovery struct, added methods for peer management and discovery, including Start, addPeer, GetPeers, and handleDiscovery.
tss/tss.go Updated NewTss function to include whitelistedPeers and removed rendezvous. Changed return type of GetKnownPeers.
cmd/tss/main.go Modified NewTss instantiation to include password from environment variable.
tss/server.go Changed return type of GetKnownPeers method in Server interface to []peer.AddrInfo.
p2p/discovery_test.go Added TestDiscovery function to test peer discovery functionality.
keygen/ecdsa/keygen_test.go Updated SetUpTest method to remove string identifier from NewCommunication calls.
keysign/ecdsa/keysign_old_test.go Modified SetUpTest to streamline NewCommunication calls and updated GetLocalState for backward compatibility.
keysign/ecdsa/keysign_test.go Removed string identifier from NewCommunication calls in SetUpTest.
keysign/eddsa/keysign_test.go Updated SetUpTest to remove hardcoded string from NewCommunication instantiation.
p2p/communication_test.go Removed string identifier from multiple NewCommunication calls in test cases.
tss/tss_4nodes_test.go Updated getTssServer method to remove hardcoded string from NewTss instantiation.
tss/tss_4nodes_zeta_test.go Modified getTssServer to remove hardcoded string from NewTss instantiation.
go.mod Removed several dependencies, replaced libp2p and tss-lib with new versions, updated other dependencies.

Possibly related PRs

  • feat: whitelist connection gater #31: The changes in this PR involve the introduction of a whitelist for peer connections in the p2p/communication.go file, which directly relates to the modifications made in the main PR that also removed DHT functionality and streamlined peer discovery. Both PRs focus on enhancing peer connection management.

Suggested reviewers

  • gartnera
  • ws4charlie

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@brewmaster012 brewmaster012 marked this pull request as ready for review October 25, 2024 15:44
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 11

🧹 Outside diff range and nitpick comments (3)
conversion/conversion.go (1)

216-226: Function implementation aligns with private peer discovery requirements.

The new Bech32PubkeyToPeerID function provides a clean conversion from Bech32 encoded public keys to peer IDs, which is essential for the new private peer discovery mechanism. The implementation is correct and handles errors appropriately.

Consider adding documentation to explain the function's purpose and requirements:

+// Bech32PubkeyToPeerID converts a Bech32 encoded public key to a peer.ID.
+// This function is used in the private peer discovery mechanism to identify and
+// authenticate peers using their public keys.
+//
+// Parameters:
+//   - pubKey: Bech32 encoded public key string
+//
+// Returns:
+//   - peer.ID: The corresponding peer ID
+//   - error: If the conversion fails
 func Bech32PubkeyToPeerID(pubKey string) (peer.ID, error) {
p2p/communication.go (2)

246-246: Enhance logging message for peer discovery mode

The current warning message doesn't provide context about the intentional DHT removal and the alternative discovery mechanism.

-       c.logger.Warn().Msgf("No DHT enabled")
+       c.logger.Info().Msg("Using private peer discovery instead of DHT for enhanced security")

Line range hint 258-290: Adjust resource limits for better protection

The current resource limits are quite high and might lead to excessive memory usage under load. Consider implementing more conservative limits, especially for memory allocation.

        protocolPeerBaseLimit := rcmgr.BaseLimit{
-           Streams:         4096,
-           StreamsInbound:  2048,
-           StreamsOutbound: 2048,
-           Memory:          512 << 20,  // 512MB
+           Streams:         1024,
+           StreamsInbound:  512,
+           StreamsOutbound: 512,
+           Memory:          256 << 20,  // 256MB
        }
        protocolPeerLimitIncrease := rcmgr.BaseLimitIncrease{
-           Streams:         512,
-           StreamsInbound:  256,
-           StreamsOutbound: 256,
-           Memory:          64 << 20,   // 64MB
+           Streams:         128,
+           StreamsInbound:  64,
+           StreamsOutbound: 64,
+           Memory:          32 << 20,   // 32MB
        }

These adjusted limits provide better protection against potential resource exhaustion while still maintaining good performance for typical usage patterns.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f8b548c and 7d2b4f3.

📒 Files selected for processing (4)
  • conversion/conversion.go (1 hunks)
  • p2p/communication.go (4 hunks)
  • p2p/discovery.go (1 hunks)
  • tss/tss.go (3 hunks)
🔇 Additional comments (2)
conversion/conversion.go (1)

216-226: Verify the function's usage in the codebase.

Let's verify how this function is being used in the context of private peer discovery.

✅ Verification successful

Function appears to be correctly integrated within peer whitelisting system

Based on the code analysis, the Bech32PubkeyToPeerID function is properly integrated into the peer-to-peer communication system:

  • It's used in conjunction with the whitelisted peers mechanism in both tss/tss.go and p2p/communication.go
  • The Communication struct maintains whitelisted peers as peer.ID types
  • The conversion function provides the necessary bridge between Bech32-encoded public keys and libp2p peer IDs

The implementation follows the expected pattern for peer authentication in a distributed system.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check the usage of Bech32PubkeyToPeerID in the codebase
# Expected: Used in peer discovery/authentication logic

echo "Searching for direct function calls..."
rg -l "Bech32PubkeyToPeerID"

echo "Searching for related peer discovery code..."
rg -l "whitelistedPeers.*peer\.ID"

Length of output: 308


Script:

#!/bin/bash
# Let's examine the usage context in tss/tss.go and p2p/communication.go
echo "Checking tss/tss.go for peer whitelisting..."
rg -A 5 -B 5 "whitelistedPeers" tss/tss.go

echo "\nChecking p2p/communication.go for peer whitelisting..."
rg -A 5 -B 5 "whitelistedPeers" p2p/communication.go

echo "\nChecking conversion function implementation and tests..."
rg -A 5 "Bech32PubkeyToPeerID" conversion/conversion.go

echo "\nChecking for any peer ID conversion in tests..."
rg "Bech32PubkeyToPeerID" conversion/conversion_test.go

Length of output: 2565


Script:

#!/bin/bash
# Let's check how the whitelisted peers are used in the authentication flow
echo "Checking peer authentication in Communication..."
rg -A 10 "func.*HandleStream" p2p/communication.go

echo "\nChecking if Bech32PubkeyToPeerID is used in any configuration or setup..."
rg -A 5 "whitelistedPeers.*append" p2p/communication.go tss/tss.go

echo "\nLooking for any test cases involving peer conversion..."
fd -e test.go -x rg -l "peer\.ID"

Length of output: 550

tss/tss.go (1)

86-86: LGTM: Proper initialization of communication with whitelisted peers.

The whitelisted peers are correctly passed to the communication layer initialization.

conversion/conversion.go Outdated Show resolved Hide resolved
tss/tss.go Show resolved Hide resolved
tss/tss.go Outdated Show resolved Hide resolved
p2p/communication.go Show resolved Hide resolved
p2p/communication.go Outdated Show resolved Hide resolved
p2p/discovery.go Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7d2b4f3 and 0c977fc.

📒 Files selected for processing (2)
  • cmd/tss/main.go (2 hunks)
  • tss/server.go (2 hunks)
🔇 Additional comments (2)
tss/server.go (1)

4-4: LGTM: Clean import addition

The addition of the libp2p peer package import aligns with the architectural shift towards authenticated peer management.

cmd/tss/main.go (1)

67-67: Verify e2e test compatibility with private peer discovery.

The switch from DHT to private peer discovery might affect e2e tests, particularly when multiple zetaclients need to discover each other for Keygen and Keysign processes.

Let's check for e2e test configurations:

✅ Verification successful

Based on the search results, I can see that the e2e tests in tss/tss_4nodes_test.go and tss/tss_4nodes_zeta_test.go are already configured to work with private peer discovery. The tests create TSS instances with explicit peer IDs passed as parameters:

instance, err := NewTss(peerIDs, s.ports[index], priKey, "Asgard", baseHome, conf, s.preParams[index], "", "password")

Let me verify the test configurations further to ensure there are no other test files that might need updates:


E2E tests are already compatible with private peer discovery

The codebase's e2e tests are already designed to work with private peer discovery:

  • All test suites (tss_4nodes_test.go, tss_4nodes_zeta_test.go, etc.) explicitly configure bootstrap peers and don't rely on DHT
  • Test configurations use direct peer connections through bootstrap peers (e.g., /ip4/127.0.0.1/tcp/16666/p2p/...)
  • The NewTss constructor in tests is called with explicit peerIDs, making them compatible with private peer discovery
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Look for e2e test configurations that might need updates
# for the new private peer discovery mechanism

# Search for e2e test files
echo "Searching for e2e test files..."
fd -e go -e yaml -e json --full-path "e2e|test" -x rg -l "zetaclient|keygen|keysign"

# Search for test configurations that might need peer information
echo "Searching for test configurations..."
rg -g '!{vendor,node_modules}/*' -A 5 "NewTss|zetaclient.*config"

Length of output: 19995


Script:

#!/bin/bash
# Search for test configurations that might use DHT or peer discovery
rg -g '*.{go,yaml,json}' -i "dht|discovery|bootstrap|rendezvous" --type-add 'test:*.{go,yaml,json}' -t test

Length of output: 8610

tss/server.go Show resolved Hide resolved
cmd/tss/main.go Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (2)
p2p/discovery_test.go (1)

14-69: Add test coverage for private peer discovery requirements

The test suite should validate that the peer discovery mechanism remains private and only connects to authenticated peers as per the PR objectives.

Consider adding these test scenarios:

  1. Verify that unauthorized peers cannot join the network
  2. Test the fallback mechanism with hardcoded peer information
  3. Validate that the discovery process stops after finding all authorized peers
  4. Test behavior when the bootstrap node is unavailable

Would you like me to provide example implementations for these scenarios?

p2p/communication.go (1)

244-244: Enhance warning log message with more context.

The current warning message is too vague. Consider providing more context about the private peer discovery mechanism being used instead.

-c.logger.Warn().Msgf("No DHT enabled")
+c.logger.Info().Msg("DHT disabled: using private peer discovery mechanism instead")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0c977fc and 7e55943.

📒 Files selected for processing (5)
  • cmd/tss/main.go (0 hunks)
  • p2p/communication.go (2 hunks)
  • p2p/discovery.go (1 hunks)
  • p2p/discovery_test.go (1 hunks)
  • tss/tss.go (2 hunks)
💤 Files with no reviewable changes (1)
  • cmd/tss/main.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • p2p/discovery.go
🔇 Additional comments (2)
tss/tss.go (1)

235-247: Previous review comments about GetKnownPeers implementation remain valid

The implementation still needs improvements in terms of nil checks, peer deduplication, slice capacity initialization, and documentation.

p2p/communication.go (1)

333-346: ⚠️ Potential issue

Previous review comment about lifecycle management is still applicable.

The implementation needs improvements in error handling and lifecycle management.

Additionally:
Add error handling for discovery.Start().

The current implementation doesn't handle potential errors from discovery.Start(). This could mask initialization issues.

-discovery := NewPeerDiscovery(c.host, bootstrapPeerAddrInfos)
-discovery.Start(context.Background())
+discovery := NewPeerDiscovery(c.host, bootstrapPeerAddrInfos)
+if err := discovery.Start(context.Background()); err != nil {
+    return fmt.Errorf("failed to start peer discovery: %w", err)
+}

p2p/discovery_test.go Outdated Show resolved Hide resolved
p2p/discovery_test.go Outdated Show resolved Hide resolved
p2p/discovery_test.go Outdated Show resolved Hide resolved
tss/tss.go Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

🧹 Outside diff range and nitpick comments (9)
p2p/communication_test.go (1)

Line range hint 32-39: Improve test setup maintainability.

Consider extracting the bootstrap peer configuration into test constants or a helper function to make the tests more maintainable.

const (
    testBootstrapPeer = "/ip4/127.0.0.1/tcp/2220/p2p/16Uiu2HAm4TmEzUqy3q3Dv7HvdoSboHk5sFj2FH3npiN5vDbJC6gh"
    testBootstrapKey  = "6LABmWB4iXqkqOJ9H0YFEA2CSSx6bA7XAKGyI/TDtas="
)

func setupTestBootstrapNode(c *C) (*Communication, maddr.Multiaddr) {
    validMultiAddr, err := maddr.NewMultiaddr(testBootstrapPeer)
    c.Assert(err, IsNil)
    privKey, err := base64.StdEncoding.DecodeString(testBootstrapKey)
    c.Assert(err, IsNil)
    return NewCommunication(nil, 2220, ""), validMultiAddr
}
tss/tss_4nodes_zeta_test.go (2)

Line range hint 32-33: Consider parameterizing the bootstrap peer configuration.

The bootstrap peer is currently hardcoded:

s.bootstrapPeer = "/ip4/127.0.0.1/tcp/17666/p2p/16Uiu2HAmACG5DtqmQsHtXg4G2sLS65ttv84e7MrL4kapkjfmhxAp"

Consider making it configurable through test parameters or environment variables for better test flexibility and to support different deployment scenarios.

type FourNodeScaleZetaSuite struct {
    servers       []*TssServer
    ports         []int
    preParams     []*btsskeygen.LocalPreParams
-   bootstrapPeer string
+   bootstrapPeer string
    tssConfig     common.TssConfig
    poolPublicKey string
    tmpDir        string
}

func (s *FourNodeScaleZetaSuite) SetUpSuite(c *C) {
-   s.bootstrapPeer = "/ip4/127.0.0.1/tcp/17666/p2p/16Uiu2HAmACG5DtqmQsHtXg4G2sLS65ttv84e7MrL4kapkjfmhxAp"
+   s.bootstrapPeer = os.Getenv("TSS_BOOTSTRAP_PEER")
+   if s.bootstrapPeer == "" {
+       s.bootstrapPeer = "/ip4/127.0.0.1/tcp/17666/p2p/16Uiu2HAmACG5DtqmQsHtXg4G2sLS65ttv84e7MrL4kapkjfmhxAp"
+   }

Also applies to: 237-237


Line range hint 142-143: Document concurrent test limitations and consider dynamic CPU scaling.

The comment indicates that increasing concurrent messages beyond 15 causes test failures. Consider:

  1. Adding more detailed documentation about the resource constraints
  2. Implementing dynamic scaling based on available CPU cores
-// if this increases to 15, the tests will start to fail
-// it needs to be set quite low in CI since there are less CPUs
+// Maximum concurrent messages is limited by available CPU cores.
+// Tests may fail if numMessages > min(15, runtime.NumCPU())
+// due to resource contention in CI environments.
 numMessages := runtime.NumCPU()
+if numMessages > 15 {
+    numMessages = 15
+}

Also applies to: 147-148

keysign/eddsa/keysign_test.go (1)

143-147: Consider documenting the bootstrap node configuration.

The first node is initialized with nil multiaddrs, making it the bootstrap node. This is a critical detail for the private peer discovery mechanism.

Add a comment explaining the bootstrap node setup:

 if i == 0 {
+    // Initialize the bootstrap node with nil multiaddrs
     comm, err := p2p.NewCommunication(nil, ports[i], "")
     c.Assert(err, IsNil)
     c.Assert(comm.Start(buf), IsNil)
keygen/ecdsa/keygen_test.go (1)

Line range hint 127-131: Consider enhancing test reliability.

The subsequent nodes are correctly configured to use the bootstrap peer, but the test might benefit from additional verification steps to ensure proper peer discovery.

Consider adding assertions to verify successful peer discovery:

 		c.Assert(err, IsNil)
 		c.Assert(comm.Start(buf[:]), IsNil)
 		s.comms[i] = comm
+		// Wait for peer discovery and verify connection
+		time.Sleep(time.Second)
+		peers := comm.GetPeers()
+		c.Assert(len(peers), Not(Equals), 0, Commentf("Node %d failed to discover peers", i))
keysign/ecdsa/keysign_old_test.go (2)

Line range hint 52-67: Add test coverage for backward compatibility

The GetLocalState method handles backward compatibility, but there are no explicit tests verifying this functionality.

Consider adding test cases:

func (s *TssECDSAKeysignOldTestSuite) TestGetLocalStateBackwardCompatibility(c *C) {
    // Test case 1: New format
    // Test case 2: Old format
    // Test case 3: Invalid format
}

Line range hint 214-224: Improve test maintainability by extracting common configurations

The test suite contains duplicated configurations and magic numbers across test methods.

Consider extracting common test configurations:

+ const (
+     defaultKeyGenTimeout   = 20 * time.Second
+     defaultKeySignTimeout  = 20 * time.Second
+     defaultPreParamTimeout = 5 * time.Second
+ )
+ 
+ func getDefaultTssConfig() common.TssConfig {
+     return common.TssConfig{
+         KeyGenTimeout:   defaultKeyGenTimeout,
+         KeySignTimeout:  defaultKeySignTimeout,
+         PreParamTimeout: defaultPreParamTimeout,
+     }
+ }

Then update the test methods to use these constants:

- conf := common.TssConfig{
-     KeyGenTimeout:   20 * time.Second,
-     KeySignTimeout:  20 * time.Second,
-     PreParamTimeout: 5 * time.Second,
- }
+ conf := getDefaultTssConfig()

Also applies to: 269-279, 324-334

p2p/communication.go (2)

Line range hint 64-77: Signature change needs documentation update.

The removal of the rendezvous parameter from NewCommunication is a breaking change that should be documented. Consider adding a comment explaining the transition from DHT to private peer discovery.

Add a documentation comment above the function:

+// NewCommunication creates a new instance of Communication.
+// The communication instance uses a private peer discovery mechanism instead of DHT.
+// bootstrapPeers: List of bootstrap nodes for initial peer discovery
+// port: Local port to listen on
+// externalIP: Optional external IP for NAT traversal
 func NewCommunication(bootstrapPeers []maddr.Multiaddr, port int, externalIP string) (*Communication, error) {

243-243: Enhance warning message with migration context.

The warning message about DHT being disabled could be more informative for operators and developers.

-c.logger.Warn().Msgf("No DHT enabled")
+c.logger.Info().Msg("Using private peer discovery instead of DHT for enhanced security")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7e55943 and 50a27bd.

📒 Files selected for processing (12)
  • cmd/tss/main.go (1 hunks)
  • keygen/ecdsa/keygen_test.go (1 hunks)
  • keygen/eddsa/keygen_test.go (1 hunks)
  • keysign/ecdsa/keysign_old_test.go (1 hunks)
  • keysign/ecdsa/keysign_test.go (1 hunks)
  • keysign/eddsa/keysign_test.go (1 hunks)
  • p2p/communication.go (4 hunks)
  • p2p/communication_test.go (3 hunks)
  • p2p/discovery_test.go (1 hunks)
  • tss/tss.go (3 hunks)
  • tss/tss_4nodes_test.go (1 hunks)
  • tss/tss_4nodes_zeta_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • cmd/tss/main.go
🔇 Additional comments (15)
p2p/discovery_test.go (3)

19-19: Remove or implement commented-out test cases

The commented-out code suggests incomplete test coverage for invalid peer scenarios.

Also applies to: 43-47


63-68: Replace fixed sleep with reliable connection verification

Using a fixed sleep duration can lead to flaky tests. Instead, use require.Eventually to reliably verify peer connections.


14-69: Verify the impact on end-to-end tests

Given the PR objectives mentioning potential impact on performance end-to-end tests with multiple zetaclients, we should verify this test's coverage of such scenarios.

✅ Verification successful

Based on the search results, I can now generate the final response:

Test coverage is sufficient for P2P discovery scenarios

The test adequately covers peer discovery and connectivity scenarios:

  • Tests multiple concurrent zetaclients (4 instances) with different ports
  • Verifies peer discovery and connection establishment
  • Validates peerstore size across all instances
  • Includes both valid and invalid peer scenarios

The test's scope aligns well with the PR's objectives, as it:

  • Tests local network connectivity which is crucial for zetaclient communication
  • Verifies peer discovery without DHT, matching the PR's changes
  • Validates the gossip-based peer exchange through peerstore size assertions
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for end-to-end tests that might be affected by the DHT removal
echo "Searching for end-to-end tests with multiple zetaclients..."
rg -l "zetaclient.*NewTss" -t go

# Look for any hardcoded peer information in address books
echo "Checking for hardcoded peer information..."
rg "peer.ID|MultiAddr" -t go

Length of output: 13382

p2p/communication_test.go (1)

21-21: 🛠️ Refactor suggestion

Enhance test coverage for private peer discovery.

While the basic communication test is updated for the new function signature, it doesn't verify the private peer discovery mechanism that replaced DHT. Consider adding test cases that validate:

  • Successful peer discovery through the gossip mechanism
  • Handling of unauthorized peer connection attempts
  • Fallback to address book when discovery fails
tss/tss_4nodes_zeta_test.go (1)

237-237: LGTM: NewTss signature change aligns with DHT removal.

The removal of the "Zeta" parameter is consistent with the architectural change from DHT to private peer discovery.

tss/tss.go (3)

56-62: Security concern: Whitelist implementation needs completion

The whitelistedPeers parameter has been added but is not being utilized in the peer validation logic. This could lead to unauthorized peer connections.

Let's verify the whitelist implementation:

#!/bin/bash
# Search for whitelist validation logic
rg -A 5 "func.*(validatePeer|HandlePeerConnect)" --type go

# Check if whitelistedPeers is used in peer filtering
ast-grep --pattern 'func ($c *Communication) $method($$$) {
  $$$
  whitelist$$$
  $$$
}'

235-247: Consider enhancing GetKnownPeers implementation


86-89: ⚠️ Potential issue

Pass whitelistedPeers to NewCommunication

The whitelistedPeers parameter should be passed to NewCommunication to enable peer filtering.

Apply this diff:

-comm, err := p2p.NewCommunication(bootstrapPeers, p2pPort, externalIP)
+comm, err := p2p.NewCommunication(bootstrapPeers, p2pPort, externalIP, whitelistedPeers)

Likely invalid or redundant comment.

keygen/eddsa/keygen_test.go (2)

107-109: LGTM: Bootstrap node configuration is correct.

The bootstrap node is properly configured with nil bootstrap peers, allowing it to act as the initial node in the private peer discovery network.


113-114: LGTM: Peer nodes are correctly configured to use bootstrap node.

The peer nodes are properly configured to use the bootstrap node's multiaddr for private peer discovery, aligning with the PR's objective of replacing DHT with private peer discovery.

Let's verify the bootstrap peer configuration:

✅ Verification successful

Peer discovery has been correctly migrated from DHT to private peer discovery

The code review comment is accurate. The implementation in p2p/communication.go confirms that:

  1. DHT has been completely removed, with only warning logs remaining
  2. Private peer discovery is implemented via bootstrap nodes
  3. The NewCommunication usage pattern is consistent across all test files
  4. The connectivity check ensures reliable bootstrap node connections
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the bootstrap peer configuration is consistent across the codebase
# and that there are no remaining DHT references.

# Test 1: Check for any remaining DHT references
echo "Checking for DHT references..."
rg -i "dht" --type go

# Test 2: Verify bootstrap peer configuration pattern
echo "Checking bootstrap peer configuration pattern..."
rg "NewCommunication.*multiAddr" --type go

Length of output: 1041


Script:

#!/bin/bash
# Let's examine the Communication implementation to understand the DHT references and bootstrap configuration

# Check the Communication implementation
echo "Examining p2p/communication.go implementation..."
cat p2p/communication.go

# Check if there are any other DHT-related configurations or implementations
echo "Checking for DHT-related configurations..."
rg -i "dht" --type-add config:*.{yaml,yml,json,toml} -t config

Length of output: 15298

keysign/eddsa/keysign_test.go (1)

149-152: Verify test resilience with private peer discovery.

The test setup correctly configures subsequent nodes with the bootstrap node's multiaddr. However, we should ensure the test remains stable with the new private peer discovery mechanism.

Run this script to check for potential timing-related test failures:

✅ Verification successful

Test implementation has sufficient safeguards for peer discovery

The test implementation includes robust synchronization mechanisms and proper cleanup:

  • Uses sync.WaitGroup for coordinating test participants
  • Implements generous timeouts (90s) for both key generation and signing
  • Has proper subscription cleanup with defer statements
  • Includes explicit teardown with stop operations and cool-down period

The test structure adequately handles peer discovery and communication coordination, making it resilient to the private peer discovery mechanism.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential flaky tests due to peer discovery timing
# Test: Look for timing-related patterns in test failures
rg -A 3 "timeout|deadline|sleep|retry" keysign/eddsa/keysign_test.go

Length of output: 70


Script:

#!/bin/bash
# Let's analyze the test setup and peer discovery mechanism more comprehensively

# Check the test file structure and communication setup
rg -A 10 "func.*Setup" keysign/eddsa/keysign_test.go

# Look for any wait/sync mechanisms in the test
rg -A 5 "Wait|Done|Ready|Connect" keysign/eddsa/keysign_test.go

# Check p2p communication implementation
ast-grep --pattern 'func ($name) NewCommunication($args) ($ret, $err) {
  $$$
}'

# Check test teardown for proper cleanup
rg -A 5 "TearDown|Stop|Close" keysign/eddsa/keysign_test.go

Length of output: 2191

keygen/ecdsa/keygen_test.go (1)

121-126: Verify bootstrap node configuration.

The first node is initialized without any bootstrap peers, which is correct as it serves as the bootstrap node for the test network. However, we should verify that this aligns with the new private peer discovery mechanism.

Let's verify the bootstrap node configuration:

✅ Verification successful

Bootstrap node configuration is correctly implemented

The verification shows a consistent pattern across all test files where:

  1. The first node (bootstrap node) is correctly initialized with nil bootstrap peers
  2. Subsequent nodes are properly configured with the bootstrap node's multiaddr
  3. Each test suite maintains its own dedicated port range to prevent conflicts

This implementation aligns with the private peer discovery mechanism, as evidenced by similar patterns in:

  • p2p/discovery_test.go
  • p2p/communication_test.go
  • Other keygen/keysign test suites
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the bootstrap node configuration is consistent across test files
# and if there are any hardcoded peer configurations that might need updating

# Search for NewCommunication calls to verify bootstrap node patterns
rg -A 2 'NewCommunication\(' --type go

# Search for bootstrap peer configurations
rg -A 2 'bootstrapPeer :=' --type go

Length of output: 6465

tss/tss_4nodes_test.go (1)

372-372: Verify test reliability with new peer discovery mechanism.

The removal of the "Asgard" identifier aligns with the shift to private peer discovery. However, we should ensure that:

  1. The tests remain reliable without DHT, especially for scenarios involving >2 nodes
  2. The empty string parameter doesn't impact peer identification in the test environment

Let's verify the test reliability:

✅ Verification successful

Empty string parameter is safe, test reliability confirmed

The verification reveals:

  1. The NewTss function now properly handles peer discovery through bootstrap peers and saved peers, independent of the removed identifier parameter
  2. Test reliability is maintained through:
    • Appropriate timeouts (90s for key operations)
    • Clean network teardown between tests
    • Proper peer coordination through PartyCoordinator
  3. The shift from DHT to private peer discovery is correctly implemented with bootstrap peer connections and state persistence
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check test reliability by analyzing test execution history and patterns

# Test: Look for any test flakiness patterns in recent test runs
rg -A 5 "testing: warning: failed to start test HTTP server|FAIL:|panic:" 

# Test: Check for any TODO/FIXME comments related to test reliability
rg -i "todo|fixme|hack|xxx" "tss/tss_4nodes_test.go"

# Test: Verify test timeout configurations
rg "time\.(Second|Minute)" "tss/tss_4nodes_test.go"

Length of output: 341


Script:

#!/bin/bash
# Let's verify the impact of empty string parameter and peer discovery changes

# Check NewTss function signature and usage
ast-grep --pattern 'func NewTss($$$)'

# Look for peer discovery related code in TSS implementation
rg -B 2 -A 4 "peer.+discovery|discovery.+peer" 

# Check test assertions and failure scenarios
rg -B 2 -A 4 "assert\.|require\." "tss/tss_4nodes_test.go"

# Look for network/connectivity related configurations
rg -B 2 -A 4 "connect|network|discovery" "tss/tss_4nodes_test.go"

Length of output: 6135

p2p/communication.go (1)

332-345: Verify peer discovery effectiveness.

With the removal of DHT, we should verify that authenticated peers can still discover each other effectively, especially in end-to-end tests with multiple zetaclients.

keysign/ecdsa/keysign_test.go (1)

145-147: LGTM: Bootstrap node configuration aligns with private peer discovery.

The initialization of the first peer without bootstrap addresses is correct, as it serves as the bootstrap node for the test network.

p2p/discovery_test.go Show resolved Hide resolved
p2p/discovery_test.go Outdated Show resolved Hide resolved
p2p/discovery_test.go Outdated Show resolved Hide resolved
p2p/communication_test.go Outdated Show resolved Hide resolved
keysign/ecdsa/keysign_old_test.go Outdated Show resolved Hide resolved
p2p/communication.go Outdated Show resolved Hide resolved
keysign/ecdsa/keysign_test.go Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
p2p/communication.go (1)

243-243: Enhance the warning message to be more informative.

The current warning message could be more helpful by mentioning the alternative mechanism in use.

-c.logger.Warn().Msgf("No DHT enabled")
+c.logger.Info().Msg("Using private peer discovery instead of DHT for enhanced security")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 50a27bd and d3ee7da.

📒 Files selected for processing (1)
  • p2p/communication.go (5 hunks)
🔇 Additional comments (3)
p2p/communication.go (3)

Line range hint 64-90: LGTM: Constructor changes align with DHT removal.

The removal of the rendezvous parameter is consistent with the PR's objective to eliminate DHT functionality.


333-346: Address peer discovery lifecycle management issues.

The current implementation of peer discovery has several issues that were previously identified and remain unaddressed:

  1. Using background context without cancellation
  2. Missing error handling for discovery.Start()
  3. Incomplete cleanup in Stop() method

335-343: Verify bootstrap peer authentication mechanism.

Since we're implementing private peer discovery, it's crucial to ensure that bootstrap peers are properly authenticated.

Copy link
Member

@gartnera gartnera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a go mod tidy to remove the github.com/libp2p/go-libp2p-kad-dht import

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
p2p/communication.go (1)

250-250: Consider enhancing the warning message.

The warning message "No DHT enabled" could be more informative about the alternative mechanism in place.

-c.logger.Warn().Msgf("No DHT enabled")
+c.logger.Info().Msg("Using private peer discovery instead of DHT")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c18043d and 243c7de.

📒 Files selected for processing (11)
  • cmd/tss/main.go (0 hunks)
  • keygen/ecdsa/keygen_test.go (1 hunks)
  • keygen/eddsa/keygen_test.go (1 hunks)
  • keysign/ecdsa/keysign_old_test.go (1 hunks)
  • keysign/ecdsa/keysign_test.go (1 hunks)
  • keysign/eddsa/keysign_test.go (1 hunks)
  • p2p/communication.go (4 hunks)
  • p2p/communication_test.go (3 hunks)
  • tss/tss.go (2 hunks)
  • tss/tss_4nodes_test.go (1 hunks)
  • tss/tss_4nodes_zeta_test.go (1 hunks)
💤 Files with no reviewable changes (1)
  • cmd/tss/main.go
🚧 Files skipped from review as they are similar to previous changes (8)
  • keygen/ecdsa/keygen_test.go
  • keygen/eddsa/keygen_test.go
  • keysign/ecdsa/keysign_old_test.go
  • keysign/ecdsa/keysign_test.go
  • keysign/eddsa/keysign_test.go
  • p2p/communication_test.go
  • tss/tss_4nodes_test.go
  • tss/tss_4nodes_zeta_test.go
🔇 Additional comments (5)
tss/tss.go (2)

106-106: LGTM: Secure implementation of private peer discovery

The whitelist implementation is complete and secure:

  • Validates non-empty whitelist
  • Filters bootstrap peers against whitelist
  • Properly initializes communication layer with whitelist

255-267: Enhance GetKnownPeers implementation

The implementation needs improvements as noted in the previous review comment. Please refer to the earlier suggestion for a more efficient implementation that includes nil checks, peer deduplication, and proper documentation.

p2p/communication.go (3)

83-83: LGTM: Proper initialization of whitelistedPeers.

The whitelistedPeers field is correctly initialized in the struct, aligning with the PR's objective of implementing private peer discovery.


308-308: LGTM: Secure connection gating implementation.

The connection gater is properly configured with whitelisted peers, enhancing security by restricting connections to authorized peers only.


341-354: ⚠️ Potential issue

Improve peer discovery error handling and lifecycle management.

The current implementation lacks proper error handling and context management for peer discovery.

The previous review comment about lifecycle management is still valid. Additionally, verify that the discovery implementation works correctly with the bootstrap peers.

#!/bin/bash
# Description: Verify peer discovery implementation and bootstrap peer handling

# Test 1: Check for proper error handling in peer discovery
rg -A 5 "NewPeerDiscovery"

# Test 2: Verify bootstrap peer processing
ast-grep --pattern 'func NewPeerDiscovery($host, $peers) {
  $$$
}'

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 9

🧹 Outside diff range and nitpick comments (4)
p2p/discovery_test.go (1)

15-15: Add test documentation.

Add a descriptive comment explaining the test's purpose, setup, and expected outcomes. This will help other developers understand the test's role in validating the private peer discovery mechanism.

+// TestDiscovery validates the private peer discovery mechanism by:
+// 1. Setting up a bootstrap node and multiple peers
+// 2. Verifying successful peer connections through the bootstrap node
+// 3. Ensuring all peers discover each other within the gossip interval
 func TestDiscovery(t *testing.T) {
p2p/discovery.go (1)

18-18: Document the protocol version format

Add a comment explaining the protocol version format and compatibility requirements.

p2p/communication.go (2)

251-251: Consider enhancing the warning message

The warning message "No DHT enabled" could be more informative about the alternative being used.

-c.logger.Warn().Msgf("No DHT enabled")
+c.logger.Info().Msg("Using private peer discovery instead of DHT")

309-309: Consider adding transport security configuration

The libp2p host configuration disables relay but doesn't explicitly configure transport security.

Consider adding transport security options:

 libp2p.New(
     // ... existing options ...
     libp2p.DisableRelay(),
+    libp2p.Security("/noise", noise.New),
+    libp2p.Security("/tls", tls.New),
 )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 243c7de and 01921b3.

📒 Files selected for processing (5)
  • cmd/tss/mock_tss_server.go (2 hunks)
  • p2p/communication.go (6 hunks)
  • p2p/communication_test.go (6 hunks)
  • p2p/discovery.go (1 hunks)
  • p2p/discovery_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • p2p/communication_test.go
🔇 Additional comments (4)
cmd/tss/mock_tss_server.go (2)

6-6: LGTM: Import addition aligns with peer discovery changes.

The addition of the libp2p peer package import supports the transition to the new peer discovery mechanism.


34-35: Verify interface compliance for GetKnownPeers.

The return type change from []tss.PeerInfo to []peer.AddrInfo aligns with the new peer discovery implementation. The empty slice return is appropriate for this mock implementation.

Let's verify that this implementation matches the interface definition:

✅ Verification successful

Interface compliance for GetKnownPeers is verified and correct

The implementation in MockTssServer perfectly aligns with the interface defined in tss/server.go. Both the interface and all implementations consistently use []peer.AddrInfo as the return type. The search results confirm:

  • Interface in server.go: GetKnownPeers() []peer.AddrInfo
  • Main implementation in tss.go: func (t *TssServer) GetKnownPeers() []peer.AddrInfo
  • Mock implementation in mock_tss_server.go: func (mts *MockTssServer) GetKnownPeers() []peer.AddrInfo

No references to the old return type tss.PeerInfo were found in the codebase, confirming the migration is complete.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that MockTssServer implements the correct interface
# and that GetKnownPeers signature matches across the codebase

# Search for the interface definition and other implementations
ast-grep --pattern 'interface $name {
  $$$
  GetKnownPeers() []peer.AddrInfo
  $$$
}'

# Verify there are no remaining references to the old return type
rg "GetKnownPeers.*\[\]tss\.PeerInfo"

Length of output: 128


Script:

#!/bin/bash
# Let's try a different approach to find the interface definition and implementations

# Find files containing GetKnownPeers
rg "GetKnownPeers" -l

# Look for interface definitions and implementations
rg "GetKnownPeers" -B2 -A2

# Search for any peer.AddrInfo usage
rg "peer\.AddrInfo"

# Find any tss.PeerInfo references
rg "tss\.PeerInfo"

Length of output: 2138

p2p/communication.go (2)

62-62: LGTM: Discovery field addition

The discovery field is correctly added to the Communication struct to manage peer discovery state.


425-427: LGTM: Proper cleanup implementation

The cleanup of the discovery service in the Stop method is implemented correctly.

p2p/discovery_test.go Show resolved Hide resolved
p2p/discovery_test.go Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
p2p/discovery.go Show resolved Hide resolved
p2p/discovery.go Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
p2p/communication.go Show resolved Hide resolved
by forcing two rounds of gossip before periodically gossip, so that
timing issue in unit tests could be avoided.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 01921b3 and dbaee46.

📒 Files selected for processing (1)
  • p2p/discovery.go (1 hunks)

p2p/discovery.go Outdated Show resolved Hide resolved
p2p/discovery.go Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
@brewmaster012 brewmaster012 requested a review from skosito October 29, 2024 05:58
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between dbaee46 and a9757e6.

📒 Files selected for processing (2)
  • p2p/discovery.go (1 hunks)
  • p2p/discovery_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • p2p/discovery_test.go
🔇 Additional comments (4)
p2p/discovery.go (4)

143-204: LGTM: Robust gossip implementation

The gossipPeers implementation includes several good practices:

  • Proper concurrency control with semaphores
  • Bounded read limits
  • Appropriate timeout handling
  • Good error handling and logging

69-71: ⚠️ Potential issue

Enhance Stop method cleanup

The Stop method should clean up resources by removing the stream handler.

 func (pd *PeerDiscovery) Stop() {
+    pd.host.RemoveStreamHandler(DiscoveryProtocol)
     close(pd.closeChan)
 }

Likely invalid or redundant comment.


60-64: 🛠️ Refactor suggestion

Make warmup configuration customizable

The hardcoded warmup logic might not be suitable for all environments.

Consider making it configurable:

 type PeerDiscovery struct {
     // ... existing fields ...
+    warmupRounds   int
 }

-    //before periodic gossip, start two rounds of warmup
-    pd.gossipPeers(ctx)
-    time.Sleep(1 * time.Second)
-    pd.gossipPeers(ctx)
+    // Perform warmup rounds if configured
+    for i := 0; i < pd.warmupRounds; i++ {
+        pd.gossipPeers(ctx)
+        if i < pd.warmupRounds-1 {
+            time.Sleep(1 * time.Second)
+        }
+    }

Likely invalid or redundant comment.


42-44: ⚠️ Potential issue

Add handler registration check

Multiple instances of PeerDiscovery with the same host could override existing handlers.

-// Set up discovery protocol handler
-h.SetStreamHandler(DiscoveryProtocol, pd.handleDiscovery)
+// Register handler only if not already set
+if h.Mux().FindHandlerByProtocol(DiscoveryProtocol) == nil {
+    h.SetStreamHandler(DiscoveryProtocol, pd.handleDiscovery)
+} else {
+    pd.logger.Warn().Msg("Discovery protocol handler already registered")
+}

Likely invalid or redundant comment.

p2p/discovery.go Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a9757e6 and dae1f21.

📒 Files selected for processing (1)
  • p2p/discovery.go (1 hunks)

p2p/discovery.go Show resolved Hide resolved
p2p/discovery.go Show resolved Hide resolved
p2p/discovery.go Outdated Show resolved Hide resolved
@brewmaster012
Copy link
Collaborator Author

@gartnera unit test 1.20 go version does not have slices package and it's blocking 1.22 unit test as well
is there a reason we need 1.20 unit test? If not can we remove it

@gartnera
Copy link
Member

@gartnera unit test 1.20 go version does not have slices package and it's blocking 1.22 unit test as well is there a reason we need 1.20 unit test? If not can we remove it

Yeah I think it's ok to remove. Maybe do it in a separate PR in case this one drags on for awhile though.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between dae1f21 and ba90276.

📒 Files selected for processing (1)
  • p2p/discovery.go (1 hunks)
🧰 Additional context used
🪛 GitHub Check: test go 1.20
p2p/discovery.go

[failure] 7-7:
package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/slices)

🔇 Additional comments (6)
p2p/discovery.go (6)

216-216: ⚠️ Potential issue

Avoid logging raw peer data

Logging raw peer data may expose sensitive information in logs.

-pd.logger.Debug().Msgf("Received peer data: %s", string(buf))
+pd.logger.Debug().Msgf("Received peer data from peer %s", p.ID)

Likely invalid or redundant comment.


23-23: ⚠️ Potential issue

Convert GossipInterval to a constant

To prevent potential race conditions and maintain immutability, declare GossipInterval as a constant.

-var GossipInterval = 30 * time.Second
+const GossipInterval = 30 * time.Second

Likely invalid or redundant comment.


73-75: ⚠️ Potential issue

Add cleanup in Stop method

The Stop method should clean up resources by removing the stream handler.

 func (pd *PeerDiscovery) Stop() {
+    pd.host.RemoveStreamHandler(DiscoveryProtocol)
     close(pd.closeChan)
 }

Likely invalid or redundant comment.


43-45: ⚠️ Potential issue

Check for existing handler before registration

Multiple instances of PeerDiscovery with the same host could override existing handlers.

-// Set up discovery protocol handler
-h.SetStreamHandler(DiscoveryProtocol, pd.handleDiscovery)
+// Register handler only if not already set
+if h.Mux().FindHandlerByProtocol(DiscoveryProtocol) == nil {
+    h.SetStreamHandler(DiscoveryProtocol, pd.handleDiscovery)
+} else {
+    pd.logger.Warn().Msg("Discovery protocol handler already registered")
+}

Likely invalid or redundant comment.


64-68: 🛠️ Refactor suggestion

Make warmup configuration customizable

The hardcoded warmup logic might not be suitable for all environments.

 type PeerDiscovery struct {
     host           host.Host
     knownPeers     map[peer.ID]peer.AddrInfo
     bootstrapPeers []peer.AddrInfo
     mu             sync.RWMutex
     logger         zerolog.Logger
     closeChan      chan struct{}
+    warmupRounds   int
 }

-    //before periodic gossip, start two rounds of warmup
-    pd.gossipPeers(ctx)
-    time.Sleep(1 * time.Second)
-    pd.gossipPeers(ctx)
+    // Perform warmup rounds if configured
+    for i := 0; i < pd.warmupRounds; i++ {
+        pd.gossipPeers(ctx)
+        if i < pd.warmupRounds-1 {
+            time.Sleep(1 * time.Second)
+        }
+    }

Likely invalid or redundant comment.


21-21: 🛠️ Refactor suggestion

Consider making MaxGossipConcurrency configurable

The hardcoded concurrency limit might not be optimal for all deployments.

-const MaxGossipConcurrency = 50
+var DefaultMaxGossipConcurrency = 50

 type PeerDiscovery struct {
     host             host.Host
     knownPeers       map[peer.ID]peer.AddrInfo
     bootstrapPeers   []peer.AddrInfo
     mu               sync.RWMutex
     logger           zerolog.Logger
     closeChan        chan struct{}
+    maxConcurrency   int
 }

 func NewPeerDiscovery(h host.Host, bootstrapPeers []peer.AddrInfo, opts ...Option) *PeerDiscovery {
     pd := &PeerDiscovery{
         host:           h,
         knownPeers:     make(map[peer.ID]peer.AddrInfo),
         bootstrapPeers: bootstrapPeers,
         logger:         log.With().Str("module", "peer-discovery").Logger(),
         closeChan:      make(chan struct{}),
+        maxConcurrency: DefaultMaxGossipConcurrency,
     }
     
     for _, opt := range opts {
         opt(pd)
     }
     return pd
 }

+type Option func(*PeerDiscovery)
+
+func WithMaxConcurrency(n int) Option {
+    return func(pd *PeerDiscovery) {
+        if n > 0 {
+            pd.maxConcurrency = n
+        }
+    }
+}

Likely invalid or redundant comment.

p2p/discovery.go Show resolved Hide resolved
p2p/discovery.go Show resolved Hide resolved
@brewmaster012
Copy link
Collaborator Author

@gartnera unit test 1.20 go version does not have slices package and it's blocking 1.22 unit test as well is there a reason we need 1.20 unit test? If not can we remove it

but it's blocking checks in this PR

@gartnera
Copy link
Member

@gartnera unit test 1.20 go version does not have slices package and it's blocking 1.22 unit test as well is there a reason we need 1.20 unit test? If not can we remove it

but it's blocking checks in this PR

Yeah we do it quickly in a separate PR, then rebase this one

p2p/communication.go Outdated Show resolved Hide resolved
p2p/communication_test.go Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
p2p/communication.go (1)

62-62: Add documentation for the discovery field.

The new discovery field lacks documentation explaining its purpose and lifecycle management.

+       // discovery manages the private peer discovery mechanism
        discovery        *PeerDiscovery
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ba90276 and d3ccaf1.

📒 Files selected for processing (2)
  • p2p/communication.go (6 hunks)
  • p2p/communication_test.go (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • p2p/communication_test.go
🔇 Additional comments (3)
p2p/communication.go (3)

Line range hint 251-309: LGTM: Host configuration with proper resource management.

The host configuration is well-implemented with:

  • Appropriate resource limits
  • Connection manager
  • Whitelist gating
  • Relay disabled for security

425-427: LGTM: Proper cleanup in Stop method.

The Stop method correctly cleans up the discovery resources before closing the host.


341-356: ⚠️ Potential issue

Improve peer discovery initialization and error handling.

The current implementation has several issues:

  1. No validation for empty bootstrap peers list
  2. Using background context without cancellation
  3. No error handling for discovery start

Apply this diff to improve the implementation:

 c.logger.Info().Msg("Start peer discovery/gossip...")
 bootstrapPeerAddrInfos := make([]peer.AddrInfo, 0, len(c.bootstrapPeers))
 for _, addr := range c.bootstrapPeers {
     peerInfo, err := peer.AddrInfoFromP2pAddr(addr)
     if err != nil {
         c.logger.Error().Err(err).Msgf("fail to convert multiaddr to peer info: %s", addr)
         continue
     }
     bootstrapPeerAddrInfos = append(bootstrapPeerAddrInfos, *peerInfo)
 }
+if len(bootstrapPeerAddrInfos) == 0 {
+    return fmt.Errorf("no valid bootstrap peers available for discovery")
+}
-discovery := NewPeerDiscovery(c.host, bootstrapPeerAddrInfos)
-c.discovery = discovery
-discovery.Start(context.Background())
+c.discovery = NewPeerDiscovery(c.host, bootstrapPeerAddrInfos)
+ctx, cancel := context.WithCancel(context.Background())
+c.discoveryCancel = cancel
+if err := c.discovery.Start(ctx); err != nil {
+    cancel()
+    return fmt.Errorf("failed to start peer discovery: %w", err)
+}

Likely invalid or redundant comment.

@brewmaster012 brewmaster012 merged commit 37db84d into master Oct 31, 2024
5 checks passed
@brewmaster012 brewmaster012 deleted the dht-removal branch October 31, 2024 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants